package com.lagou.service;

import java.util.List;


import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

public class RpcDecoder  extends ByteToMessageDecoder {

    private Class<?> clazz;

    private Serializer serializer;



    public RpcDecoder(Class<?> clazz, Serializer serializer) {

        this.clazz = clazz;

        this.serializer = serializer;

    }

	@Override
	protected void decode(ChannelHandlerContext arg0, ByteBuf byteBuf, List<Object> arg2) throws Exception {
		
            byte[] bytes = new byte[byteBuf.readableBytes()];
            byteBuf.readBytes(bytes);
            Object o = serializer.deserialize(clazz, bytes);
            arg2.add(o);
	}

}
